package com.leff.cards.util;

import java.io.IOException;

import android.content.Context;
import android.content.SharedPreferences;

public class Settings {

	private static SharedPreferences sSettings;

	public static final int FB_POST_NEVER = 0;
	public static final int FB_POST_PROMPT = 1;
	public static final int FB_POST_ALWAYS = 2;
	
	public static void init(Context C) throws IOException {
		if(sSettings != null) {
			return;
		}
		sSettings = C.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE);
	}
	
	public static String getAccount() {
		return sSettings.getString(ACCOUNT_KEY, null);
	}
	public static void setAccount(String email) {
		sSettings.edit().putString(ACCOUNT_KEY, email).commit();
	}
	
	public static String getFBAccessToken() {
		return sSettings.getString(FB_TOKEN_KEY, null);
	}
	public static void setFBAccessToken(String token) {
		sSettings.edit().putString(FB_TOKEN_KEY, token).commit();
	}
	
	public static long getFBExpiration() {
		return sSettings.getLong(FB_EXPIRE_KEY, -1);
	}
	public static void setFBExpiration(long time) {
		sSettings.edit().putLong(FB_EXPIRE_KEY, time).commit();
	}
	
	public static void clearFacebookCredentials() {
		sSettings.edit().remove(FB_EXPIRE_KEY).remove(FB_TOKEN_KEY).commit();
	}
	
	public static boolean getNotificationsEnabled() {
		return sSettings.getBoolean(NOTIFICATIONS_KEY, true);
	}
	public static void setNotificationsEnabled(boolean enabled) {
		sSettings.edit().putBoolean(NOTIFICATIONS_KEY, enabled).commit();
	}
	public static void clearNotificationsEnabled() {
		sSettings.edit().remove(NOTIFICATIONS_KEY).commit();
	}
	
	public static boolean getPostToFacebook() {
		return sSettings.getBoolean(FB_POST_KEY, true);
	}
	public static void setPostToFacebook(boolean enabled) {
		sSettings.edit().putBoolean(FB_POST_KEY, enabled).commit();
	}
	
	public static int getInvitePostPreference() {
		return sSettings.getInt(FB_POST_INVITE_KEY, FB_POST_PROMPT);
	}
	public static void setInvitePostPreference(int pref) {
		sSettings.edit().putInt(FB_POST_INVITE_KEY, pref).commit();
	}
	
	public static int getVotePostPreference() {
		return sSettings.getInt(FB_POST_VOTE_KEY, FB_POST_PROMPT);
	}
	public static void setVotePostPreference(int pref) {
		sSettings.edit().putInt(FB_POST_VOTE_KEY, pref).commit();
	}
	
	public static boolean needsUpgrade() {
		return sSettings.getBoolean(UPGRADE_KEY, false);
	}
	public static void setUpgrade(boolean upgrade) {
		sSettings.edit().putBoolean(UPGRADE_KEY, upgrade).commit();
	}
	
	private static final String PREFERENCES_KEY = "CardsSettings";
	private static final String ACCOUNT_KEY = "account";
	private static final String FB_TOKEN_KEY = "fb_access";
	private static final String FB_EXPIRE_KEY = "fb_expire";
	private static final String NOTIFICATIONS_KEY = "notifs";
	
	private static final String FB_POST_KEY = "do_fb_post";
	private static final String FB_POST_INVITE_KEY = "do_invite_post";
	private static final String FB_POST_VOTE_KEY = "do_vote_post";
	
	private static final String UPGRADE_KEY = "upgrade";
}
